package com.help10000.rms.mvp.presenter

import com.alibaba.fastjson.JSON
import com.amir.common.api.CustomObserver
import com.amir.common.mvp.model.Model
import com.amir.common.mvp.presenter.BasePresenter
import com.amir.common.utils.Constant
import com.amir.common.utils.SPUtils
import com.help10000.rms.mvp.view.RefreshTokenView
import com.google.gson.JsonElement
import okhttp3.MediaType
import okhttp3.RequestBody

open class RefreshTokenPresenter(view: RefreshTokenView) : BasePresenter<RefreshTokenView>(view) {


    fun refreshToken() {

        val refresh_token = SPUtils.getString(Constant.Refresh_token)
        val map = mutableMapOf<String, Any>()
        map.put("acc", SPUtils.getString(Constant.accoun).toString())
        map.put("pwd", SPUtils.getString(Constant.pwd).toString())
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                JSON.toJSON(map).toString()
            )
        val refreshToken = Model.getServer().refreshToken(refresh_token, "grant_type", body)
        Model.getObservable(refreshToken, object : CustomObserver<JsonElement>(mvpView) {

            override fun success(t: JsonElement) {
                mvpView?.refreshTokenSuccess(t)
            }
        })
    }

    fun checkToken() {
        val token = SPUtils.getString(Constant.AccessToken)
        val accountid = SPUtils.getString(Constant.accountid).toString()
        val checkToken = Model.getServer().checkToken(token, accountid)
        Model.getObservable(checkToken, object : CustomObserver<JsonElement>(mvpView) {

            override fun success(t: JsonElement) {

            }
        })
    }
}